package com.zlll.winner.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.LoginUser;
import com.zlll.winner.business.model.sysUser.SysPermissionRole;
import com.zlll.winner.business.model.sysUser.SysRole;
import com.zlll.winner.business.service.sysUser.ISysMenuService;
import com.zlll.winner.business.service.sysUser.ISysPermissionRoleService;
import com.zlll.winner.business.service.sysUser.ISysRoleService;
import com.zlll.winner.common.BaseController;
import com.zlll.winner.common.BaseResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 活动管理控制器
 */
@RestController
@RequestMapping(value = "/sys/role")
public class SysRoleController extends BaseController {

    @Reference
    private ISysMenuService iSysMenuService;

    @Reference
    private ISysRoleService iSysRoleService;

    @Reference
    private ISysPermissionRoleService iSysPermissionRoleService;

    /**
     * 分页查询权限
     * @param data {name:""}
     * @return
     */
    @PostMapping(value = "list")
    public BaseResult page(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        SysRole role = new SysRole();
        role.setName(data.getString("name"));
        Integer pageNo = data.getInteger("pageNo") == null ? 1 : data.getInteger("pageNo");
        Integer pageSize = data.getInteger("pageSize") == null ? 20 : data.getInteger("pageSize");
        IPage<SysRole> pages = iSysRoleService.findRolePage(role, pageNo, pageSize);
        baseResult.setData(pages);
        return baseResult;
    }

    /**
     * 保存功能权限
     * 2017-5-9 下午2:25:55
     * @param data
     * @author lubingbing
     */
    @PostMapping(value = "save")
    @LoginUser
    public BaseResult save(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        SysRole role = data.toJavaObject(SysRole.class);
        List<Long> menus = data.getJSONArray("menus").toJavaList(Long.class);
        iSysRoleService.addMenuRole(role, menus, getUserId(), getUser().getName());
        return baseResult;
    }

    /**
     * 获取菜单打钩的数据权限
     * @param data
     * @return
     */
    @PostMapping(value = "menuPermission")
    public BaseResult menuPermission(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        Long id = data.getLong("id");
        List<SysPermissionRole> list = iSysPermissionRoleService.getPermissionRoleList(id);
        baseResult.setData(list);
        return baseResult;
    }

    /**
     * 更新功能权限方法
     * 2017-5-9 下午2:25:55
     * @author lubingbing
     * @return
     */
    @PostMapping(value = "update")
    @LoginUser
    public BaseResult update(@RequestBody JSONObject data){
        BaseResult baseResult = new BaseResult();
        SysRole role = data.toJavaObject(SysRole.class);
        List<Long> menus = data.getJSONArray("menus").toJavaList(Long.class);
        iSysRoleService.updateMenuRole(role,menus, getUserId(), getUser().getName());
        return baseResult;
    }

    /**
     * 删除数据
     * @param data
     * @return
     */
    @PostMapping(value = "delete")
    @LoginUser
    public BaseResult deleteAnn(@RequestBody JSONObject data) {
        BaseResult baseResult = new BaseResult();
        String[] ids = data.getJSONArray("ids").toJavaObject(String[].class);
        for(String id: ids){
            iSysPermissionRoleService.deletePermissionRole(Long.parseLong(id), getUserId(), getUser().getName());
        }
        return baseResult;
    }

    /**
     * 获取所有的角色
     * @return
     */
    @PostMapping(value = "allList")
    public BaseResult allList() {
        BaseResult baseResult = new BaseResult();
        List<SysRole> list = iSysRoleService.getAllList();
        baseResult.setData(list);
        return baseResult;
    }
}
